package org.ruoyi.dhuman.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.common.core.validate.AddGroup;
import org.ruoyi.common.core.validate.EditGroup;
import org.ruoyi.common.idempotent.annotation.RepeatSubmit;
import org.ruoyi.common.log.annotation.Log;
import org.ruoyi.common.log.enums.BusinessType;
import org.ruoyi.common.web.core.BaseController;
import org.ruoyi.core.page.PageQuery;
import org.ruoyi.core.page.TableDataInfo;
import org.ruoyi.dhuman.domain.bo.DhumanBo;
import org.ruoyi.dhuman.domain.vo.DhumanVo;
import org.ruoyi.dhuman.service.IDhumanService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数字人
 *
 * @author wangliang
 * @date Tue Oct 14 12:06:06 CST 2025
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/dhuman")
public class DhumanController extends BaseController {

    private final IDhumanService dhumanService;

	/**
	 * 查询数字人列表
	 */
	@SaCheckPermission("dhuman:dhuman:list")
	@GetMapping("/list")
    public R<TableDataInfo<DhumanVo>> list(DhumanBo bo, PageQuery pageQuery) {
		TableDataInfo<DhumanVo> result = dhumanService.queryPageList(bo, pageQuery);
		return R.ok(result);
    }

    /**
     * 导出数字人列表
     */
//    @SaCheckPermission("dhuman:dhuman:export")
//    @Log(title = "数字人", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(DhumanBo bo, HttpServletResponse response) {
//        List<DhumanVo> list = dhumanService.queryList(bo);
//        ExcelUtil.exportExcel(list, "数字人", DhumanVo.class, response);
//    }

    /**
     * 获取数字人详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("dhuman:dhuman:query")
    @GetMapping("/{id}")
    public R<DhumanVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(dhumanService.queryById(id));
    }

    /**
     * 新增数字人
     */
    @SaCheckPermission("dhuman:dhuman:add")
    @Log(title = "数字人", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DhumanBo bo) {
        return toAjax(dhumanService.insertByBo(bo));
    }

    /**
     * 修改数字人
     */
    @SaCheckPermission("dhuman:dhuman:edit")
    @Log(title = "数字人", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DhumanBo bo) {
        return toAjax(dhumanService.updateByBo(bo));
    }

    /**
     * 删除数字人
     *
     * @param ids 主键串
     */
    @SaCheckPermission("dhuman:dhuman:remove")
    @Log(title = "数字人", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(dhumanService.deleteWithValidByIds(List.of(ids), true));
    }
}
